package com.lanyou.esb.web.serviceMonitor;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.util.gson.GsonUtil;
import com.lanyou.esb.common.CommonUtils;
import com.lanyou.esb.service.PfThroughputService;
import com.lanyou.esb.vo.PfThroughputVo;
import com.lanyou.esb.vo.PfThroughputVo.Series;
import com.lanyou.esb.web.param.PfThroughputParam;

/**
 * 平台吞吐量/吞吐率控制器
 * 
 * @author Davey.wu
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/pfThroughput")
public class PfThroughputController {
	@Autowired
	private PfThroughputService pfThroughputService;

	/**
	 * 查找平台吞吐量
	 * 
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/findPfThroughputInfo", produces = "application/json", method = RequestMethod.POST)
	public String findPfThroughputInfo(@RequestBody PfThroughputParam param) {
		Map<String, Object> result = new HashMap<>(2);
		// 查询结束时间点
		Date now = new Date();
		try {
			// 获取平台吞吐量
			PfThroughputVo vo = pfThroughputService.findPfThroughputInfo(
					param.getSelectAll(), param.getFields(), param.getPeriod(),
					now);
			// 格式化结果集
			if (vo != null && vo.getSeries() != null) {
				for (Series serie : vo.getSeries()) {
					List<Double> list = serie.getValues();
					if (list == null || list.isEmpty()) {
						continue;
					}
					// 结果转换为MB,并保留两位小数
					for (int i = 0; i < list.size(); ++i) {
						// 转为MB
						BigDecimal b = new BigDecimal(list.get(i) / (1024));
						// 保留两位小数
						list.set(i, b.setScale(2, BigDecimal.ROUND_HALF_UP)
								.doubleValue());
					}
				}
			}
			result.put(CommonUtils.MSG_FIELD_RESULT, true);
			result.put(CommonUtils.MSG_FIELD_VALUE, vo);
		} catch (Exception e) {
			result.put(CommonUtils.MSG_FIELD_RESULT, false);
			result.put(CommonUtils.MSG_FIELD_MSG, e.getMessage());
		}
		return GsonUtil.getDefaultGson().toJson(result);
	}

	/**
	 * 查找平台吞吐率
	 * 
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/findPfThroughputRateInfo", produces = "application/json", method = RequestMethod.POST)
	public String findPfThroughputRateInfo(@RequestBody PfThroughputParam param) {
		Map<String, Object> result = new HashMap<>(2);
		// 查询结束时间点
		Date now = new Date();
		try {
			// 获取平台吞吐率
			PfThroughputVo vo = pfThroughputService.findPfThroughputRateInfo(
					param.getSelectAll(), param.getFields(), param.getPeriod(),
					now);
			// 格式化结果集
			if (vo != null && vo.getSeries() != null) {
				for (Series serie : vo.getSeries()) {
					List<Double> list = serie.getValues();
					if (list == null || list.isEmpty()) {
						continue;
					}
					// 结果转换为MB,并保留两位小数
					for (int i = 0; i < list.size(); ++i) {
						// 转为MB
						BigDecimal b = new BigDecimal(list.get(i) / (1024));
						// 保留两位小数
						list.set(i, b.setScale(2, BigDecimal.ROUND_HALF_UP)
								.doubleValue());
					}
				}
			}
			result.put(CommonUtils.MSG_FIELD_RESULT, true);
			result.put(CommonUtils.MSG_FIELD_VALUE, vo);
		} catch (Exception e) {
			result.put(CommonUtils.MSG_FIELD_RESULT, false);
			result.put(CommonUtils.MSG_FIELD_MSG, e.getMessage());
		}
		return GsonUtil.getDefaultGson().toJson(result);
	}

}
